package org.example.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.admin.dto.CategoryTree;
import org.example.admin.service.AdminGoodsCategoryService;
import org.example.common.utils.ResponseUtil;
import org.example.db.entity.CategoryFirst;
import org.example.db.entity.CategorySecond;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods/category")
@Api(tags = "对商品类目的操作")
public class AdminGoodsCategoryController {

    AdminGoodsCategoryService adminGoodsCategoryService;

    public AdminGoodsCategoryController(AdminGoodsCategoryService adminGoodsCategoryService) {
        this.adminGoodsCategoryService = adminGoodsCategoryService;
    }

    @RequestMapping("/list")
    public Object getCategoryList(){
        return ResponseUtil.ok(adminGoodsCategoryService.getCategoryList());
    }

    @RequestMapping("/list2")
    public Object getCategory2List(){
        return ResponseUtil.ok(adminGoodsCategoryService.getCategoryList2());
    }

    @PostMapping("/delete")
    public Object deleteCategory(@RequestBody CategoryFirst categoryFirst){
        Long id = categoryFirst.getId();
        adminGoodsCategoryService.deleteCategory(id);
        adminGoodsCategoryService.deleteCategory2s(id);
        return ResponseUtil.ok();
    }

    @PostMapping("/delete2")
    public Object deleteCategory2(@RequestBody CategorySecond categorySecond){
        adminGoodsCategoryService.deleteCategory2(categorySecond.getId());
        return ResponseUtil.ok();
    }

    @PostMapping("/update")
    public Object updateCategory(@RequestBody CategoryFirst categoryFirst){
        adminGoodsCategoryService.updateCy1(categoryFirst);
        return ResponseUtil.ok();
    }

    @PostMapping("/create")
    public Object createCategory(@RequestBody CategoryFirst categoryFirst){
        adminGoodsCategoryService.createCy1(categoryFirst);
        return ResponseUtil.ok();
    }

    @PostMapping("/update2")
    public Object updateCategory2(@RequestBody CategorySecond categorySecond){
        adminGoodsCategoryService.updateCy2(categorySecond);
        return ResponseUtil.ok();
    }

    @PostMapping("/create2")
    public Object createCategory2(@RequestBody CategorySecond categorySecond){
        if(adminGoodsCategoryService.createCy2(categorySecond)==1){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @GetMapping("/list/tree")
    @ApiOperation("获得一二级分类的树状结构")
    public Object listTree(){

        List<CategoryTree> categoryTrees = adminGoodsCategoryService.listTree();

        return ResponseUtil.ok(categoryTrees);
    }

}
